package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation;
import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relations;

/**
 * Servlet implementation class Entity
 */
public class RelationFavorite extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RelationFavorite() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("favorite") != null) {
			json = request.getParameter("favorite");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int start = ((Long) regtJSON.get("start")).intValue();
			int length = ((Long) regtJSON.get("length")).intValue();
			String ds_type = (String) regtJSON.get("ds_type");
			int ds_id = Integer.parseInt((String) regtJSON.get("ds_id"));
			String user = (String) regtJSON.get("user");
			String lang = (String) regtJSON.get("lang");
			
			int totalNum = 0;		
			totalNum = Relations.getFavoriteNum(ds_type, ds_id, user, lang);
			
			ArrayList<Relation> array = Relations.getFavorite(ds_type, ds_id,
					user, start, length, lang);
			JSONArray elements = new JSONArray();
			for (Relation item : array) {
				JSONObject jsob = new JSONObject();
				jsob.put("id", item.getRelationID());
				jsob.put("name", item.getName());
//				jsob.put("user", item.getUser());
				jsob.put("group", item.getGroup());
				jsob.put("time", item.getDate());
				jsob.put("lang", item.getLang());
				String roles = "";
				for (String role : item.getRoles()) {
					roles += role + ",";
				}
				roles = roles.substring(0, roles.length() - 1);
				jsob.put("roles", roles);
				elements.add(jsob);
			}
			JSONObject result = new JSONObject();
			result.put("result", elements);
			result.put("totalNum", totalNum);
			response.getWriter().print(result);
		}
	}
}
